//
//  FCAddRelationTypeViewController.m
//  customerRelation
//
//  Created by 陈文斌 on 2019/6/12.
//

#import "FCAddRelationGroupViewController.h"
#import "CustomerRelationHeader.h"
#import "TopNoticeLabel.h"
#import "FCAddRelationGroupSubViewController.h"
#import "FCAlertViewController.h"
#import "FCCustomerRelationRequest.h"
#import "UIViewController+returnBlock.h"

@interface FCAddRelationGroupViewController ()
@property (nonatomic, strong) UIButton *addRelationTypeButton;
@property (nonatomic, strong) FCAddRelationGroupSubViewController *addRelationGroupSubVC;

@end

@implementation FCAddRelationGroupViewController

// 初始化UI
- (void)setupUI{
    WJCWeakSelf
    if (self.relationType) {
        self.title = customerRelationLocaChanged(@"分组管理", @"分组管理");
        [self.view addSubview:[self createManagerRelationTypeButton]];
    } else{
        self.title = customerRelationLocaChanged(@"添加分组", @"添加分组");
        [self.view addSubview:[self createAddRelationTypeButton]];
    }
    self.view.backgroundColor = FCBackColor;
    
    TopNoticeLabel *topNoticeLabel = [[TopNoticeLabel alloc] init];
    topNoticeLabel.noticeTxt = customerRelationLocaChanged(@"分组负责人可以管理该分组的上下游关系", @"分组负责人可以管理该分组的上下游关系");
    topNoticeLabel.noticeLabel.font = font13;
    // 计算高度
    CGSize maximumLabelSize = CGSizeMake(WJCScreenW - (16 * 2 + 20 + 10), CGFLOAT_MAX);// 16:内边距;20:关闭按钮宽度;10:内边距
    CGSize expectSize = [topNoticeLabel.noticeLabel sizeThatFits:maximumLabelSize];
    topNoticeLabel.frame = CGRectMake(0, 0, WJCScreenW, expectSize.height + 20);
    [self.view addSubview:topNoticeLabel];
    
    _addRelationGroupSubVC = [[FCAddRelationGroupSubViewController alloc] init];
    _addRelationGroupSubVC.relationType = self.relationType;
    [self addChildViewController:_addRelationGroupSubVC];
    [_addRelationGroupSubVC didMoveToParentViewController:self];
    [_addRelationGroupSubVC.view setFrame:CGRectMake(0, expectSize.height + 20, WJCScreenW, self.view.height - (expectSize.height + 20) - 80 - FCTabbarSafeBottomMargin)];
    [self.view addSubview:_addRelationGroupSubVC.view];

    _addRelationGroupSubVC.buttonStateBlock = ^(BOOL isEnabled) {
        [weakSelf.addRelationTypeButton setEnabled:isEnabled];
        if (isEnabled) {
            [weakSelf.addRelationTypeButton setBackgroundColor:FCBlueBackColor];
        } else{
            [weakSelf.addRelationTypeButton setBackgroundColor:FCSimpleBlueColor];
        }
    };
    
    _addRelationGroupSubVC.idBlock = ^(FCEmployeeModel *employee) {
        weakSelf.idBlock(employee);
    };
    
    topNoticeLabel.closeBlock = ^{
        XMGLog(@"关闭");
        [UIView animateWithDuration:0.25 animations:^{
            [weakSelf.addRelationGroupSubVC.view setFrame:CGRectMake(0, 0, WJCScreenW, self.view.height - 80 - FCTabbarSafeBottomMargin)];
        } completion:nil];
    };
}

#pragma mark - 创建添加按钮
- (UIView *)createAddRelationTypeButton{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, WJCScreenH - 64 - 80 - FCTabbarSafeBottomMargin, WJCScreenW, 80 + FCTabbarSafeBottomMargin)];
    view.backgroundColor = FCWhiteColor;
    view.layer.shadowColor = WJCColor(239, 241, 246).CGColor;
    view.layer.shadowOffset = CGSizeMake(0,-4);
    view.layer.shadowOpacity = 1;
    view.layer.shadowRadius = 6;
    
    self.addRelationTypeButton = [UIButton new];
    [view addSubview:_addRelationTypeButton];
    [_addRelationTypeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.top.mas_equalTo(16);
        make.right.mas_equalTo(-12);
        make.height.mas_equalTo(46);
    }];
    [_addRelationTypeButton setEnabled:NO];
    _addRelationTypeButton.backgroundColor = FCSimpleBlueColor;
    [_addRelationTypeButton setTitle:@"确定" forState:UIControlStateNormal];
    [_addRelationTypeButton setTitleColor:FCWhiteColor forState:UIControlStateNormal];
    _addRelationTypeButton.titleLabel.font = font15;
    _addRelationTypeButton.layer.cornerRadius = 4;
    [_addRelationTypeButton addTarget:self action:@selector(addRelationType:) forControlEvents:UIControlEventTouchUpInside];
    
    return view;
}

#pragma mark - 确定添加
-(void)addRelationType:(UIButton *)btn{
    [_addRelationGroupSubVC addRelationType:btn];
}

#pragma mark - 创建管理按钮
- (UIView *)createManagerRelationTypeButton{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, WJCScreenH - 64 - 80 - FCTabbarSafeBottomMargin, WJCScreenW, 80 + FCTabbarSafeBottomMargin)];
    view.backgroundColor = FCWhiteColor;
    view.layer.shadowColor = WJCColor(239, 241, 246).CGColor;
    view.layer.shadowOffset = CGSizeMake(0,-4);
    view.layer.shadowOpacity = 1;
    view.layer.shadowRadius = 6;
    
    UIButton *deleteButton = [UIButton new];
    [view addSubview:deleteButton];
    deleteButton.backgroundColor = FCWhiteColor;
    [deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    [deleteButton setTitleColor:FCRedTxtColor forState:UIControlStateNormal];
    deleteButton.titleLabel.font = font15;
    FCLRViewBorderRadius(deleteButton, 4, 1, FCRedTxtColor);
    [deleteButton addTarget:self action:@selector(deleteRelationType) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *confirmButton = [UIButton new];
    [view addSubview:confirmButton];
    confirmButton.backgroundColor = FCBlueBackColor;
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton setTitleColor:FCWhiteColor forState:UIControlStateNormal];
    confirmButton.titleLabel.font = font15;
    confirmButton.layer.cornerRadius = 4;
    [confirmButton addTarget:self action:@selector(confirmRelationType:) forControlEvents:UIControlEventTouchUpInside];
    
    if (!self.relationType.banModify) {
        [deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(16);
            make.top.mas_equalTo(16);
            make.height.mas_equalTo(46);
        }];
        
        [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(deleteButton.mas_right).mas_equalTo(23);
            make.top.mas_equalTo(16);
            make.right.mas_equalTo(-16);
            make.width.equalTo(deleteButton.mas_width).mas_equalTo(0);
            make.height.equalTo(deleteButton.mas_height).mas_equalTo(0);
        }];
    } else {
        deleteButton.hidden = YES;
        [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(16);
            make.top.mas_equalTo(16);
            make.right.mas_equalTo(-16);
            make.height.mas_equalTo(46);
//            make.width.equalTo(deleteButton.mas_width).mas_equalTo(0);
//            make.height.equalTo(deleteButton.mas_height).mas_equalTo(0);
        }];
    }
    
    
    return view;
}

#pragma mark - 确定编辑
- (void)confirmRelationType:(UIButton *)btn{
    [_addRelationGroupSubVC editRelationType:btn];
}

#pragma mark - 删除分组
- (void)deleteRelationType{
    [FCAlertViewController alertCommonWithTitle:[NSString stringWithFormat:@"确定删除“%@”关系类型？",self.relationType.name] andSubTitle:nil andController:self andCommitAction:^(FCAlertAction *action) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSDictionary *args = @{
                                   @"ids": @[self.relationType.Id]
                                   };
            [FCCustomerRelationRequest deleteRelationTypeWithArgs:args vc:self success:^(id resultObj) {
                [SVProgressHUD showSuccessWithStatus:customerRelationLocaChanged(@"global_general_delete_success", @"删除成功")];
                [saveSandBox DELETEDataWithType:nil andOther:nil andClass:[FCRelationTypeModel class]];
                [self.navigationController popViewControllerAnimated:YES];
            } failure:^(NSError *error) {
                
            }];
        });
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

@end
